<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* Echarts (https://github.com/apache/echarts)
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_summaryAttributesJobService"></title>
    <script type="text/javascript" include="echarts" src="../../dist/ol/include-ol.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            top: 10px;
            right: 10px;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <div id="infoControl" style=" position: absolute;top: 80px;left: 10px;text-align: left;"></div>
    <div id="popup" class="ol-popup" style='width:350px'>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title" data-i18n="resources.title_summaryAttributesJobService"></h3>
            </div>
            <div class="panel-body">
                <div class="input-group">
                    <span class="input-group-addon"><span data-i18n="resources.text_inputData"></span><span data-i18n="[title]resources.text_requiredField"
                            style="color: red;"> * </span>　</span>
                    <input id="datasetName" type="text" class="form-control" value="samples_processing_newyorkZone_R" />
                </div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon' data-i18n="resources.text_groupField"></span>
                    <input id='groupField' type='text' class='form-control' value='borough' />
                </div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon' data-i18n="resources.text_attributeField"></span>
                    <input id='attributeField' type='text' class='form-control' value='LocationID' />
                </div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon' data-i18n="resources.text_statisticModes"></span>
                    <input id='statisticModes' type='text' class='form-control' value='sum' />
                </div>
                <p></p>
                <div align="right">
                    <input type="button" id='btn' class="btn btn-primary" data-i18n="[value]resources.btn_summary" />
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" include="bootstrap,jquery,widgets" src="../js/include-web.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var control,
            processingsUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = new ol.Map({
                target: 'map',
                // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
                controls: ol.control.defaults.defaults({
                        attributionOptions: {
                            collapsed: false
                        }
                    })
                    .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
                view: new ol.View({
                    center: [-73.95, 40.75],
                    zoom: 12,
                    projection: 'EPSG:4326',
                    multiWorld: true
                })
            });
        map.addLayer(new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest({
                url: mapURL,
            }),
        }));

        var processingService = new ol.supermap.ProcessingService(processingsUrl, {
                withCredentials: window.isLocal
            }),
            info = new ol.control.Control({
                element: document.getElementById('popup')
            });

        info.setMap(map);
        map.addControl(info);

        ol.supermap.SecurityManager.registerToken(processingsUrl, window.exampleToken);

        $('#btn').on('click', function () {
            if ($('#msg_container')[0]) {
                $('#msg_container').remove();
            }
            widgets.loader.showLoader();
            if (map && control) {
                map.removeControl(control);
                $("#infoControl").html("");
            }
            var summaryAttributesJobsParameter = new ol.supermap.SummaryAttributesJobsParameter({
                datasetName: $('#datasetName').val(),
                groupField: $('#groupField').val(),
                attributeField: $('#attributeField').val(),
                statisticModes: $('#statisticModes').val()
            });
            processingService.addSummaryAttributesJob(summaryAttributesJobsParameter).then(function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTDATA') {
                        var dataUrl = info.serviceAddress + '/data';
                        var sqlParam = new ol.supermap.GetFeaturesBySQLParameters({
                            queryParameter: {
                                name: "analystResult@UntitledDatasource",
                                attributeFilter: "SMID > 0"
                            },
                            datasetNames: ["UntitledDatasource:analystResult"]
                        });
                        new ol.supermap.FeatureService(dataUrl).getFeaturesBySQL(sqlParam,
                            ).then(function (serviceResult) {
                                result = serviceResult.result.features.features;
                                var names = [];
                                var data1 = [];
                                var data2 = [];
                                for (var i = 0; i < result.length; i++) {
                                    names.push(result[i].properties.BOROUGH);
                                    data1.push(parseFloat(result[i].properties.RECORDCOUNT));
                                    data2.push(parseFloat(result[i].properties.SUM_LOCATIONID));
                                }
                                chart.setOption({
                                    yAxis: [{
                                            type: 'category',
                                            data: names
                                        },
                                        {
                                            type: 'category',
                                            data: names
                                        }
                                    ],
                                    series: [{
                                            name: "RECORDCOUNT",
                                            data: data1
                                        },
                                        {
                                            name: "SUM_LOCATIONID",
                                            data: data2
                                        }
                                    ]
                                });
                                control = new ol.control.Control({
                                    element: chart.getDom(),
                                    target: document.getElementById('infoControl')
                                });
                                map.addControl(control);
                            });
                        widgets.loader.removeLoader();
                    }
                });
            });
        });
        option = {
            backgroundColor: '#0E2A43',
            legend: {
                bottom: 20,
                textStyle: {
                    color: '#fff',
                },
                data: ['RECORDCOUNT', 'SUM_LOCATIONID']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '20%',
                containLabel: true
            },
            tooltip: {
                show: "true",
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            xAxis: {
                type: 'value',
                axisTick: {
                    show: false
                },
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: '#fff',
                    }
                },
                splitLine: {
                    show: false
                },
            },
            yAxis: [{
                    type: 'category',
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: '#fff',
                        }
                    }
                },
                {
                    type: 'category',
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        show: false
                    },
                    splitArea: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    }
                }
            ],
            series: [{
                    name: 'bar1',
                    type: 'bar',
                    itemStyle: {
                        normal: {
                            show: true,
                            color: '#5de3e1',
                            barBorderRadius: 50,
                            borderWidth: 0,
                            borderColor: '#333',
                        }
                    },
                    barGap: '0%',
                    barCategoryGap: '50%'
                },
                {
                    name: 'bar2',
                    type: 'bar',
                    yAxisIndex: 1,

                    itemStyle: {
                        normal: {
                            show: true,
                            color: '#277ace',
                            barBorderRadius: 50,
                            borderWidth: 0,
                            borderColor: '#333',
                        }
                    },
                    barGap: '0%',
                    barCategoryGap: '50%'
                }
            ]
        };
        var chart = echarts.init(document.createElement('div'), '', {
            width: 400,
            height: 300
        });
        chart.setOption(option);
    </script>
</body>

</html>